package com.boarsoft.flow.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FlowConfig {
	private static Logger log = LoggerFactory.getLogger(FlowConfig.class);
	private static Properties prop;

	public static String FLOWS_DIR = "./flows";
	public static String DIAGRAMS_DIR = "./diagrams";

	static {
		if (prop == null) {
			InputStream is = FlowConfig.class.getClassLoader().getResourceAsStream("conf/config.properties");
			if (is == null) {
				log.warn("Can not init FlowConfig/config.properties");
			} else {
				log.info("Init FlowConfig with default config.properties");
				FlowConfig.init(is);
			}
		}
	}

	public static void init(InputStream is) {
		if (prop == null) {
			prop = new Properties();
			try {
				prop.load(is);
				//
				FLOWS_DIR = prop.getProperty("flow.flows.dir", FLOWS_DIR);
				DIAGRAMS_DIR = prop.getProperty("flow.diagrams.dir", DIAGRAMS_DIR);
			} catch (IOException e) {
				log.error("Error on load system config.", e);
				throw new RuntimeException(e);
			}
			// TODO 配置检查？
			// if (Util.strIsEmpty(FLOWS_DIR)){
			// throw new Exception("flows.dir is invalid");
			// }
		}
	}
}